package com.example.segment_saver;

import android.annotation.TargetApi;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class DeleteSite extends ListFragment {
	
	
	private SiteList sites = new SiteList();
	private int itemSelected =-1;
	
	public void onResume(){
		super.onResume();
		setListAdapter(getCurrentSites());
	}
	public void onPause(){
		super.onPause();
		sites.save(new File(getActivity().getFilesDir(), "sites.bin"));
	}
	
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
	{
		inflater.inflate(R.menu.fragment_delete_list, menu);
	}
	
	public void onPrepareOptionsMenu(Menu menu){
		super.onPrepareOptionsMenu(menu);
		MenuItem delete = menu.findItem(R.id.menu_delete_site);
		//Dont think I will have an edit function but I could add it here
		//MenuItem edit = menu.findItem(R.id.menu_edit_site);
		if(itemSelected!=-1)
		{
			delete.setEnabled(true);
			//edit.setEnabled(true);
		}
		else
		{
			delete.setEnabled(false);
			//edit.setEnabled(false);
		}
	}
	public boolean onOptionsItemSelected (MenuItem item)
	{
		Intent intent = new Intent(getActivity(), Delete_Site.class);
		if(item.getItemId()== R.id.menu_new_site)
		{
			getActivity().startActivity(intent);
		}
		if(item.getItemId() == R.id.menu_delete_site)
		{
			TextView text = (TextView) getListView().getChildAt(itemSelected);
			remove_site(text.getText().toString());			
		}
		return true;
	}
	
	
	public void onListItemClick(ListView l, View v, int position, long id)
	{
		// I think this is where I would have to make it so I could have box's instead of radials but not sure
		itemSelected=position;
		getActivity().invalidateOptionsMenu();
	}
	
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		setEmptyText("No Sites To Remove.");
		setListAdapter(getCurrentSites());
		// TODO:Might have to change the ListView.CHOICE_MODE_SINGLE.
		getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	}
	public void remove_site(String site){
		sites.removeSite(site);
		sites.save(new File(getActivity().getFilesDir(), "sites.bin"));
		setListAdapter(getCurrentSites());
		itemSelected=-1;
		getActivity().invalidateOptionsMenu();
	}
	
	public ArrayAdapter<String> getCurrentSites()
	{
		// Made this Multiple choice as well because of the one above. 
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice );
		sites.clear();
		sites.load(new File(getActivity().getFilesDir(), "sites.bin"));
		
		for(String name : sites){
			adapter.add(name);
		}
		
		return adapter;
	}
				
}

